# Maintainer: Frederik Carlier <frederik.carlier@keysight.com>

_realname=libdispatch
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-libblocksruntime-swift")
pkgver=6.1.1
pkgrel=1
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://apple.github.io/swift-corelibs-libdispatch/"
msys2_repository_url="https://github.com/apple/swift-corelibs-libdispatch/"
license=('spdx:Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-lld")
source=("https://github.com/apple/swift-corelibs-libdispatch/archive/swift-${pkgver}-RELEASE.tar.gz"
        "001-link-libBlocksRuntime.patch")
sha256sums=('6fc6f8b1767a1348e1d960647b2bfbc52fd7074b7aeab97bd0f4b21af58baa47'
            '2b249f8478f9e63c6beb90d679d07102d0903d3ddfd28a265310d6a015b7d802')

prepare() {
  cd swift-corelibs-${_realname}-swift-${pkgver}-RELEASE

  patch -Np1 -i "${srcdir}"/001-link-libBlocksRuntime.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    LDFLAGS+=" -fuse-ld=lld" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_C_COMPILER="clang" \
      -DCMAKE_CXX_COMPILER="clang++" \
      "${extra_config[@]}" \
      -S "swift-corelibs-${_realname}-swift-${pkgver}-RELEASE" \
      -B "build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}"
}

package_libdispatch() {
  pkgdesc="The libdispatch Project, (a.k.a. Grand Central Dispatch), for concurrency on multicore hardware (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-libblocksruntime-swift"
           "${MINGW_PACKAGE_PREFIX}-libwinpthread"
           $([[ $MINGW_PACKAGE_PREFIX == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libs"))

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}"

  mkdir -p dest${MINGW_PREFIX}/{bin,lib,include}
  mv "${pkgdir}${MINGW_PREFIX}/bin/libBlocksRuntime.dll" "dest${MINGW_PREFIX}/bin/libBlocksRuntime.dll"
  mv "${pkgdir}${MINGW_PREFIX}/lib/libBlocksRuntime.dll.a" "dest${MINGW_PREFIX}/lib/libBlocksRuntime.dll.a"
  mv "${pkgdir}${MINGW_PREFIX}/include/Block.h" "dest${MINGW_PREFIX}/include/Block.h"

  install -Dm644 "swift-corelibs-${_realname}-swift-${pkgver}-RELEASE"/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}

package_libblocksruntime-swift() {
  pkgdesc="Blocks runtime library for Objective C (mingw-w64)"
  conflicts=("${MINGW_PACKAGE_PREFIX}-libblocksruntime")
  replaces=("${MINGW_PACKAGE_PREFIX}-libblocksruntime")
  provides=("${MINGW_PACKAGE_PREFIX}-libblocksruntime")

  mv dest/* "${pkgdir}"

  install -Dm644 "swift-corelibs-${_realname}-swift-${pkgver}-RELEASE"/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/libblocksruntime-swift/LICENSE
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
